<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>根据地址查询经纬度</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.3"></script>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.3&ak=aN69xM0okebmmR6iSEyXKXY8uyLjspbR"></script>
</head>
<body style="background:#CBE1FF">
    <div style="width:730px;margin:auto;">   
        要查询的地址：<input id="text_" type="text" value="珠海长隆" style="margin-right:100px;"/>
        查询结果(经纬度)：<input id="result_" type="text" />
        <input type="button" value="查询" onclick="searchByStationName();"/>
        <div id="container" 
            style="position: absolute;
                margin-top:30px; 
                width: 730px; 
                height: 590px; 
                top: 50; 
                border: 1px solid gray;
                overflow:hidden;">
        </div>
    </div>
</body>
<script type="text/javascript">
    var map = new BMap.Map("container");
    map.centerAndZoom("珠海", 12);
    map.enableScrollWheelZoom();    //启用滚轮放大缩小，默认禁用
    map.enableContinuousZoom();    //启用地图惯性拖拽，默认禁用

    map.addControl(new BMap.NavigationControl());  //添加默认缩放平移控件
    map.addControl(new BMap.OverviewMapControl()); //添加默认缩略地图控件
    map.addControl(new BMap.OverviewMapControl({ isOpen: true, anchor: BMAP_ANCHOR_BOTTOM_RIGHT }));   //右下角，打开

    var localSearch = new BMap.LocalSearch(map);
    localSearch.enableAutoViewport(); //允许自动调节窗体大小
function searchByStationName() {
    map.clearOverlays();//清空原来的标注
    var keyword = document.getElementById("text_").value;
    localSearch.setSearchCompleteCallback(function (searchResult) {
        var poi = searchResult.getPoi(0);
        document.getElementById("result_").value = poi.point.lng + "," + poi.point.lat;
        map.centerAndZoom(poi.point, 13);
        var marker = new BMap.Marker(new BMap.Point(poi.point.lng, poi.point.lat));  // 创建标注，为要查询的地址对应的经纬度
        map.addOverlay(marker);
    });
    localSearch.search(keyword);
}  
</script>
<!-- <script type="text/javascript">
	
    //定位成功事件
    //var myKeys=[];
    var myKeys='';
    var lng='';         //精度
    var lat='';         //纬度
    // 百度地图API功能
    var map = new BMap.Map("allmap");
    var point = new BMap.Point(108.95,34.27);
    map.centerAndZoom(point,12);
    var geolocation = new BMap.Geolocation();
    geolocation.getCurrentPosition(function(r){console.log(r.point)
        if(this.getStatus() == BMAP_STATUS_SUCCESS){
            var mk = new BMap.Marker(r.point);
            map.addOverlay(mk);//标出所在地
            map.panTo(r.point);//地图中心移动
            //alert('您的位置：'+r.point.lng+','+r.point.lat);
            lng=r.point.lng;
            lat=r.point.lat;
            var point = new BMap.Point(r.point.lng,r.point.lat);    //用所定位的经纬度查找所在地省市街道等信息
            var gc = new BMap.Geocoder();
            gc.getLocation(point, function(rs){
               var addComp = rs.addressComponents;
               console.log(rs.address);//地址信息
               //myKeys = [rs.address];     当有其他信息时，可以定义成数组
                myKeys = rs.address;
               //alert(myKeys);         //弹出所在地址
               var local = new BMap.LocalSearch(map, {
                   renderOptions:{map:map,panel:"r-result"},
                   pageCapacity:3
                });
                local.searchInBounds(myKeys, map.getBounds());
            });
        }else {
            alert('failed'+this.getStatus());
        }        
    },{enableHighAccuracy: true})

    /**
     * 保存
     */
    function add(){
    //alert(myKeys);
      $.ajax({
            type: 'GET',
            url: '<%=request.getContextPath()%>/customer/map/save',
            dataType: 'json',
            data: {'address':myKeys,'lng':lng,'lat':lat},   
            success : function(res){
                // 添加文字说明
                alert(res);
            }         

      }); 
  }   

    //实时刷新
    function refresh(){
        window.location.href="<%=request.getContextPath()%>/customer/map/index";
    }


</script> -->
<script type="text/javascript">
	 var poi = searchResult.getPoi(0);
var map = new BMap.Map("container");    
map.centerAndZoom(new BMap.Point(poi.point.lng + "," + poi.point.lat), 14);    
var driving = new BMap.DrivingRoute(map, {    
    renderOptions: {    
        map   : map,     
        panel : "results",    
        autoViewport: true    
    }    
});    
driving.search("东莞理工学院", "珠海长隆");</script>
</html>